# 05-ATM.py

# 声明一个读取文件的函数
def get_file():
    with open('./12-money.txt','r') as file:
        str = file.read()
        return int(str)
# money = 500 原金额就注释掉
# 主菜单函数
def hello(name):
    print(f'{name},你好欢迎来到建设银行。请选择操作：')
    while True:
        print('查询余额','[输入1]')
        print('存款\t','[输入2]')
        print('取款\t','[输入3]')
        print('退出\t','[输入4]')
        index = int(input('请输入您的选择：'))
        if index==4:break; # 结束循环
        if index==1: sel_money(name)
        if index==2:add_money(name)
        if index==3:q_money(name)
# 查询余额
def sel_money(name):
    print("----------------查询余额----------------")
    print(f'{name},你好,您的余额剩余:{get_file()}RMB')
# 存款
def add_money(name):
    a = int(input('请输入存款金额'))
    money = get_file() + a
    with open('./12-money.txt','w',encoding="utf-8") as file:
        file.write(str(money))
        file.flush() 
    print(f'{name}，你好，当前存款金额为{a}RMB')
    print(f'{name},你好，剩余金额{get_file()}RMB')
# 取款 
def q_money(name):
    a = int(input('请输入取款金额：'))
    if a>get_file():
        print('没钱还取啥')
    else:
        money=get_file()-a
        with open('./12-money.txt','w') as file:
            file.write(str(money))
            file.flush()
        print(f'请确认取款金额为{a}RMB')
        print(f'当前余额剩余：{get_file()}RMB')
# 主函数
def atm(name):
    hello(name)

atm('敦哥')